const BaseIpcHandler = require('../BaseIpcHandler');

class NewCategoryHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log('>> NewCategoryHandler params = ', params);
        const { dataset_id, category_name, supercategory, keypoints, skeleton, style } = params;

        // 业务逻辑
        let result = null;
        try {
            const lastId = await this.app.db.categories.insertCategory(
                dataset_id,
                {
                    category_name,
                    supercategory,
                    keypoints,
                    skeleton
                },
                style
            );
            console.log(`Category ${lastId} Created OK! params = `, params);
            result = { code: 0, msg: lastId };
        } catch (error) {
            console.error('Error creating category:', error.message);
            result = { code: -1, msg: error.message };
        }

        // 返回结果
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = NewCategoryHandler;
